package com.lzh.farmersshop.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lzh.farmersshop.entity.Type;
import com.lzh.farmersshop.util.Result;
import lombok.extern.slf4j.Slf4j;
import com.lzh.farmersshop.entity.Goodsimg;
import com.lzh.farmersshop.service.GoodsimgService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * (Goodsimg)表控制层
 *
 * @author makejava
 * @since 2024-01-04 15:41:50
 */
@Slf4j
@RestController
@RequestMapping("admin/goodsimg")
@Transactional(rollbackFor = Exception.class)
public class GoodsimgController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsimgService goodsimgService;

    /**
     * 分页查询所有数据
     *
     * @param goodsimg 查询实体
     * @return 所有数据
     */
    @GetMapping("selectAll")
    public Result selectAll(Goodsimg goodsimg,Integer page,Integer limit) {
        if (limit == null || page == null) {
            return Result.failure("limit和page不能为空");
        }
        LambdaQueryWrapper<Goodsimg> q = new LambdaQueryWrapper<>();
        q.eq(goodsimg.getGoodsid()!= null, Goodsimg::getGoodsid, goodsimg.getGoodsid());
        HashMap<String, Object> result = new HashMap<>();
        result.put("count", goodsimgService.list(q).size());
        //分页
        q.last("limit " + (page - 1) * limit + "," + limit);
        result.put("data", goodsimgService.list(q));
        return Result.success(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return Result.success(goodsimgService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param goodsimg 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public Result insert(Goodsimg goodsimg) {
        //判断goodsimg是否等于空，等于直接返回
        if (goodsimg==null){
            return Result.failure("图片信息不能为空");
        }
        goodsimg.setIsdelete(1);

        if (goodsimgService.save(goodsimg)){
            return Result.success("添加成功");
        }
        return Result.failure("添加失败");
    }

    /**
     * 修改数据
     *
     * @param goodsimg 实体对象
     * @return 修改结果
     */
    @PostMapping("update")
    public Result update(Goodsimg goodsimg) {
        //判断goodsimg是否等于空，等于直接返回
        if (goodsimg==null){
            return Result.failure("图片信息不能为空");
        }

        if (goodsimgService.updateById(goodsimg)){
            return Result.success("修改成功");
        }
        return Result.failure("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @PostMapping("delete")
    public Result delete(Integer id) {
        //判断id是否为空
        if (id == null) {
            return Result.failure("id不能为空");
        }

        Goodsimg goodsimg = new Goodsimg();
        goodsimg.setIsdelete(2);
        goodsimg.setId(id);

        if (goodsimgService.removeById(goodsimg)) {
            return Result.success("删除成功");
        }
        return Result.failure("删除失败");
    }
}

